import {UserLogin} from '@/api/login'
import router from '@/router';
const user={
    state:{
        name:'',
        subjectId:'',
        token:localStorage.getItem('access-token')?localStorage.getItem('access-token'):''
    },

    mutations:{
        SET_SUBJECTID:(state, subjectId) => {
            state.subjectId = subjectId
          },
        SET_TOKEN:(state, token) => {
            state.token = token
          },
        DELETE_TOKEN:(state) => {
            state.token = ''
        },
        
    },

    actions:{
        LoginUsername({commit},userInfo){
            return new Promise((reslove,reject)=>{
                UserLogin(userInfo.username,userInfo.password).then(res=>{
                    if(res.code==0){
                        commit('SET_TOKEN',res.data.token)
                        commit('SET_SUBJECTID',res.data.subjectId)
                    }
                    reslove(res)
                }).catch(err=>{
                    reject(err)
                })
            })
        },
        FedLogOut({commit}){
            commit('DELETE_TOKEN')
            router.push('/login');
        }
    }
}
export default user
